package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.missilike.biz.IMilJournalDetailService;
import com.missilike.biz.IMilPhotoService;
import com.missilike.core.service.IOSSServcice;
import com.missilike.core.utils.SecurityUtils;
import com.missilike.domain.request.MilJournalDetailForm;
import com.missilike.domain.request.MilJournalDetailStatusForm;
import com.missilike.domain.response.MilAdvertInfo;
import com.missilike.domain.response.MilStartupPageInfo;
import com.missilike.repository.entity.MilAdvert;
import com.missilike.repository.entity.MilJournalDetail;
import com.missilike.repository.mapper.MilJournalDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 启动页 服务实现类
 * </p>
 *
 * @author kin
 * @since 2018-12-25
 */
@Service
public class MilJournalDetailServiceImpl extends ServiceImpl<MilJournalDetailMapper, MilJournalDetail> implements IMilJournalDetailService {

    private final IMilPhotoService milPhotoService;

    private final IOSSServcice aliYunOSSService;

    @Autowired
    public MilJournalDetailServiceImpl(@Qualifier("OSSService") IOSSServcice aliYunOSSService, IMilPhotoService milPhotoService) {
        this.aliYunOSSService = aliYunOSSService;
        this.milPhotoService = milPhotoService;
    }

    @Transactional
    @Override
    public void upsert(MilJournalDetailForm milJournalDetailForm) {
        if (milJournalDetailForm.getFile() != null) {
            //上传图片到OSS
            String filePath = aliYunOSSService.updateImages(milJournalDetailForm.getFile());
            milJournalDetailForm.setImageUrlId(milPhotoService.upsertPhotoId(milJournalDetailForm.getImageUrlId(), filePath));
        }
        if (milJournalDetailForm.getId() == null) {
            milJournalDetailForm.setCreateUserId(SecurityUtils.getCurrentUserId());
        } else {
            milJournalDetailForm.setUpdateUserId(SecurityUtils.getCurrentUserId());
        }
        this.saveOrUpdate(milJournalDetailForm);
    }

    @Override
    public IPage<MilJournalDetail> getList(Page<MilJournalDetail> page) {
        return this.page(page, null);
    }

    @Override
    public void status(MilJournalDetailStatusForm milJournalDetailStatusForm) {
        MilJournalDetail startupPage = new MilJournalDetail();
        startupPage.setStatus(milJournalDetailStatusForm.getStatus());
        baseMapper.update(startupPage, new UpdateWrapper<MilJournalDetail>()
                .lambda()
                .in(MilJournalDetail::getId, Arrays.asList(milJournalDetailStatusForm.getIds()))
        );
    }

    @Override
    public List<MilStartupPageInfo> getInfoList(int pageNum, int pageSize){
        List<MilStartupPageInfo> milStartupPageInfos = new ArrayList<>();
        Page<MilJournalDetail> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MilJournalDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .orderByDesc("create_time");
        IPage<MilJournalDetail> milJournalDetailIPage = this.page(page, queryWrapper);
        if (milJournalDetailIPage.getRecords() != null && milJournalDetailIPage.getRecords().size() > 0) {
            milJournalDetailIPage.getRecords().forEach(milAdvert->{
                MilStartupPageInfo milStartupPageInfo = getMilStartupPageInfo(milAdvert);
                milStartupPageInfos.add(milStartupPageInfo);
            });
        }
        return milStartupPageInfos;
    }

    @Override
    public MilStartupPageInfo getNew(){
        QueryWrapper<MilJournalDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .orderByDesc("create_time");
        MilJournalDetail milStartupPage = this.getOne(queryWrapper);

        MilStartupPageInfo milAdvertInfo = getMilStartupPageInfo(milStartupPage);

        return milAdvertInfo;
    }

    private MilStartupPageInfo getMilStartupPageInfo(MilJournalDetail milStartupPage) {
        MilStartupPageInfo milAdvertInfo = new MilStartupPageInfo();
        milAdvertInfo.setCreateTime(milStartupPage.getCreateTime());
        milAdvertInfo.setCreateUserId(milStartupPage.getCreateUserId());
        milAdvertInfo.setExtras(milStartupPage.getExtras());
        milAdvertInfo.setId(milStartupPage.getId());
        milAdvertInfo.setImageUrl(milPhotoService.getPhotoUrl(milStartupPage.getImageUrlId()));
        milAdvertInfo.setRemark(milStartupPage.getRemark());
        milAdvertInfo.setStatus(milStartupPage.getStatus());
        milAdvertInfo.setType(milStartupPage.getType());
        milAdvertInfo.setUpdateTime(milStartupPage.getUpdateTime());
        milAdvertInfo.setUpdateUserId(milStartupPage.getUpdateUserId());
        return milAdvertInfo;
    }
}
